package com.gzh.animatortext.activity

import android.animation.Animator
import android.animation.AnimatorInflater
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.Button
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import com.gzh.animatortext.R
import com.gzh.animatortext.databinding.ActivityNormalBinding

/**
 * @创建者 顾朝辉
 * @创建时间 2021/5/25 9:42
 * @描述 唱片机效果
 */
class NormalActivity : AppCompatActivity(), View.OnClickListener {
    lateinit var anim_start: ObjectAnimator
    lateinit var anim_play: Animator
    lateinit var anim_pause: Animator

    lateinit var binding: ActivityNormalBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityNormalBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // 因图片原因需调整机头的旋转圆心
        binding.ivHead.pivotX = binding.ivHead.left.toFloat() + 39
        binding.ivHead.pivotY = binding.ivHead.top.toFloat() + 39
        setClick()
        initAnim()
    }

    private fun setClick() {
        binding.buttonPlay.setOnClickListener(this)
    }

    private fun initAnim() {
        //anim_start
        anim_start = ObjectAnimator.ofFloat(binding.rltVinylRecord, "rotation", 0f, 360f)
        anim_start.duration = 10000 //旋转一圈10秒
        anim_start.interpolator = LinearInterpolator() //旋转不停顿
        anim_start.repeatCount = ObjectAnimator.INFINITE //一直执行
        //anim_play
        anim_play = AnimatorInflater.loadAnimator(this, R.animator.rotate_play)
        anim_play.setTarget(binding.ivHead)
        //anim_pause
        anim_pause = AnimatorInflater.loadAnimator(this, R.animator.rotate_pause)
        anim_pause.setTarget(binding.ivHead)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.buttonPlay -> {
                if (binding.buttonPlay.text == "播放") {
                    binding.buttonPlay.text = "暂停"
                    if (anim_start.isPaused) {
                        anim_start.resume()
                        anim_play.start()
                    } else {
                        anim_start.start()
                        anim_play.start()
                    }
                } else {
                    binding.buttonPlay.text = "播放"
                    anim_start.pause()
                    anim_pause.start()
                }
            }
        }
    }
}